package com.unicloud.medical.oss.core;

import com.unicloud.medical.oss.common.Endpoint;
import com.unicloud.medical.oss.common.model.BucketInfo;
import com.unicloud.medical.oss.common.model.DownloadObject;
import com.unicloud.medical.oss.common.model.ObjectInfo;
import com.unicloud.medical.oss.common.model.request.*;
import com.unicloud.medical.oss.core.annotation.ExceptionInterceptor;
import com.unisound.medical.common.model.dto.ResultDTO;
import com.unisound.tracelog.client.annotation.TraceLog;

import java.util.List;

public class DefaultEndpoint implements Endpoint {

    private final OSSServiceFinder ossServiceFinder;

    public DefaultEndpoint(OSSServiceFinder ossServiceFinder) {
        this.ossServiceFinder = ossServiceFinder;
    }

    /**
     * 查看存储桶是否存在
     */
    @TraceLog("查看存储桶是否存在")
    @ExceptionInterceptor
    @Override
    public ResultDTO<Boolean> bucketExist(BucketExistRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).bucketExist(request));
    }

    /**
     * 创建存储桶
     */
    @TraceLog("创建存储桶")
    @ExceptionInterceptor
    @Override
    public ResultDTO<Boolean> createBucket(CreateBucketRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).createBucket(request));
    }

    /**
     * 创建默认权限存储桶
     */
    @TraceLog("创建默认权限存储桶")
    @ExceptionInterceptor
    @Override
    public ResultDTO<Boolean> createDefaultPolicyBucket(CreateBucketRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).createDefaultPolicyBucket(request));
    }

    /**
     * 删除存储桶
     */
    @TraceLog("删除存储桶")
    @ExceptionInterceptor
    @Override
    public ResultDTO<Boolean> removeBucket(DeleteBucketRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).removeBucket(request));
    }

    /**
     * 获取全部存储桶
     */
    @TraceLog("获取全部存储桶")
    @ExceptionInterceptor
    @Override
    public ResultDTO<List<BucketInfo>> getAllBuckets(CommonRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).getAllBuckets());
    }

    /**
     * 获取当前存储桶所有文件
     */
    @TraceLog("获取当前存储桶所有文件")
    @ExceptionInterceptor
    @Override
    public ResultDTO<List<ObjectInfo>> listAllObjectsCurrBucket(ListAllObjectsCurrBucketRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).listAllObjects(request));
    }

    /**
     * 获取当前存储桶当前文件夹所有文件
     */
    @TraceLog("获取当前存储桶当前文件夹所有文件")
    @ExceptionInterceptor
    @Override
    public ResultDTO<List<ObjectInfo>> listObjectsCurrBucketAndFolder(ListAllObjectsCurrBucketAndFolderRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).listObjects(request));
    }

    /**
     * 文件上传
     */
    @TraceLog("文件上传")
    @ExceptionInterceptor
    @Override
    public ResultDTO<String> uploadObject(InnerUploadObjectRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).uploadObject(request));
    }

    /**
     * 批量文件上传
     */
    @TraceLog("批量文件上传")
    @ExceptionInterceptor
    @Override
    public ResultDTO<List<String>> uploadObjects(InnerUploadObjectsRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).uploadObjects(request));
    }

    /**
     * 本地文件上传
     */
    @TraceLog("本地文件上传")
    @ExceptionInterceptor
    @Override
    public ResultDTO<String> uploadLocalObject(UploadLocalObjectRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).uploadLocalObject(request));
    }

    /**
     * 批量上传本地文件
     */
    @TraceLog("批量上传本地文件")
    @ExceptionInterceptor
    @Override
    public ResultDTO<List<String>> uploadLocalObjects(UploadLocalObjectsRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).uploadLocalObjects(request));
    }

    /**
     * 文件下载
     */
    @TraceLog(value = "文件下载", ignoreResult = true)
    @ExceptionInterceptor
    @Override
    public ResultDTO<DownloadObject> downloadObject(DownloadObjectRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).downloadObject(request));
    }

    /**
     * 下载文件夹下所有文件
     */
    @TraceLog(value = "下载文件夹下所有文件", ignoreResult = true)
    @ExceptionInterceptor
    @Override
    public ResultDTO<DownloadObject> downloadObjects(DownloadObjectsRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).downloadObjects(request));
    }

    /**
     * 根据url地址删除文件
     */
    @TraceLog("根据url地址删除文件")
    @ExceptionInterceptor
    @Override
    public ResultDTO<String> removeObject(RemoveObjectRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).removeObject(request));
    }

    /**
     * 根据url地址列表批量删除文件
     */
    @TraceLog("根据url地址列表批量删除文件")
    @ExceptionInterceptor
    @Override
    public ResultDTO<List<String>> removeObjects(RemoveObjectsRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).removeObjects(request));
    }

    /**
     * 删除当前桶当前文件夹所有文件
     */
    @TraceLog("删除当前桶当前文件夹所有文件")
    @ExceptionInterceptor
    @Override
    public ResultDTO<List<String>> removeObjects(RemoveObjectsCurrBucketAndFolderRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).removeObjects(request));
    }

    /**
     * 文件复制
     */
    @TraceLog("文件复制")
    @ExceptionInterceptor
    @Override
    public ResultDTO<String> copyObject(CopyObjectRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).copyObject(request));
    }

    /**
     * 已签名的地址
     * @param request
     */
    @Override
    public ResultDTO<String> signedUrl(SignedUrlRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).signedUrl(request));
    }

    /**
     * 批量已签名地址
     */
    @Override
    public ResultDTO<List<String>> signedUrls(SignedUrlsRequest request) {
        return ResultDTO.success(ossServiceFinder.find(request.getOssName()).signedUrls(request));
    }

}
